import React, { Component } from "react";
import { Form } from "antd";
import { FormElement } from "src/library/components";
import config from "src/commons/config-hoc";
import { ModalContent } from "src/library/components";

@config({
    ajax: true,
    modal: {
        title: (props) => (props.isEdit ? "修改用户" : "添加用户"),
    },
})
export default class EditModal extends Component {
    state = {
        loading: false, // 页面加载loading
        data: {},
    };

    componentDidMount() {
        const { isEdit, formdata } = this.props;
        console.log(formdata, "formdata");

        if (isEdit) {
            this.form.setFieldsValue(formdata);
        }
    }

    handleSubmit = async (values) => {
        console.log(values, "values");
        if (this.state.loading) return;

        const { isEdit } = this.props;
        const ajaxMethod = isEdit ? this.props.ajax.put : this.props.ajax.post;
        const successTip = isEdit ? "修改成功！" : "添加成功！";
        let resp;

        this.setState({ loading: true });
        // ajaxMethod('/mock/users', values, {successTip})
        //     .then(() => {
        //         const {onOk} = this.props;
        //         onOk && onOk();
        //     })
        //     .finally(() => this.setState({loading: false}));

        if (isEdit) {
            values.id = this.props.id;
            resp = await this.props.ajax.post(
                "admin/adminUser/update",
                values,
                { successTip, noEmpty: true },
            );
        } else {
            resp = await this.props.ajax.post(
                "admin/adminUser/add",
                values,
                { successTip, noEmpty: true },
            );
        }
        if (resp.code == 200) {
            this.setState({ loading: false });
        } else {
            console.log(this.props);
        }
        const { onOk } = this.props;
        onOk && onOk();
    };

    render() {
        const { isEdit } = this.props;
        const { loading, data } = this.state;
        const formProps = {
            labelWidth: 100,
        };
        return (
            <ModalContent
                loading={loading}
                okText="保存"
                cancelText="重置"
                onOk={() => this.form.submit()}
                onCancel={() => this.form.resetFields()}
            >
                <Form
                    ref={(form) => (this.form = form)}
                    onFinish={this.handleSubmit}
                    initialValues={data}
                >
                    {isEdit ? (
                        <FormElement {...formProps} type="hidden" name="id" />
                    ) : null}
                    <FormElement
                        {...formProps}
                        label="用户名"
                        name="app_id"
                        required
                        noSpace
                    />
                    <FormElement
                        {...formProps}
                        label="密码"
                        name="app_secret"
                        required
                    />
                    <FormElement
                        {...formProps}
                        type="select"
                        label="权限码"
                        name="scope"
                        required
                        options={[{ value: "32", label: "超级管理员" }]}
                    />
                    <FormElement
                        {...formProps}
                        label="应用描述"
                        name="app_description"
                    />
                    <FormElement
                        {...formProps}
                        label="权限描述"
                        name="scope_description"
                    />
                </Form>
            </ModalContent>
        );
    }
}
